package monster.com.cvh.fragment.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import monster.com.cvh.activity.LoginActivity;
import monster.com.cvh.util.CustomToast;
import monster.com.cvh.util.SPUtils;
import monster.com.cvh.util.ToastUtils;

/**
 * Created by 13580 on 2018/3/2.
 */

public abstract class BaseFragment extends Fragment {
    public static final String mTag = BaseFragment.class.getSimpleName();
    protected View mRootView;
    protected Unbinder mUnbinder;
    protected Context mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            onCurrentAttach(context);
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            onCurrentAttach(activity);
        }
    }

    /**
     * 重写此方法替代onAttach()高低版本不兼容
     *
     * @param context
     */
    protected void onCurrentAttach(@NonNull Context context) {
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(getLayoutResId(), container, false);
        mUnbinder = ButterKnife.bind(this, mRootView);
        return mRootView;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        initEvent();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }


    /**
     * 初始化布局文件
     *
     * @return
     */
    protected abstract int getLayoutResId();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 绑定控件事件
     */
    protected abstract void initEvent();

    protected void showToastShort(@StringRes int res) {
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        CustomToast.show(getResources().getString(res), Toast.LENGTH_SHORT);
    }

    protected void showToastLong(@StringRes int res) {
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        CustomToast.show(getResources().getString(res), Toast.LENGTH_LONG);
    }

    protected void showToastShort(String content) {
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        CustomToast.show(content, Toast.LENGTH_SHORT);
    }

    protected void showToastLong(String content) {
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        CustomToast.show(content, Toast.LENGTH_LONG);
    }

    /**
     * 检查登录
     * @return
     */
    public boolean isLogined(){
        return !SPUtils.getString(getContext(), "token", "").equals("");
    }


    /**
     * 跳转到登录页面
     */
    public void goToLogin() {
        showToastLong("登录已经过期，请退出重新登录");
        getActivity().finish();
        startActivity(new Intent(getActivity(), LoginActivity.class));
    }


}
